/**
 * System 对象
 *
 * @package js.lang
 * @author: DSONet (dsonet@msn.com)
 * @version $Id: System.js 35 2008-03-02 17:01:34Z dsonet $
 */
/**
 * 
 *
 */
var f = Type.F_FUNC
	, ua = navigator.userAgent
	, puid = 0;
f.toString = Type.F_FUNC;

/**
 * System 对象，用于判断运行环境的相关信息及PUID
 *
 * it's a static class, defined as a object.
 *
 * @package js.lang
 * @author: dawei jin(jindw@xidea.org)
 * @author: DSONet (dsonet@msn.com)
 * @version $Id: System.js 35 2008-03-02 17:01:34Z dsonet $
 */
var System = 
{
	/** 
	 * 测试当前浏览器是否为IE
	 * @public
	 * @id System.isIE
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	isIE: f
	/** 
	 * 测试当前浏览器是否为Opera
	 * @public
	 * @id System.isOpera
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isOpera: f
	/** 
	 * 测试当前浏览器是否为Gecko核心
	 * @public
	 * @id System.isGecko
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isGecko: f
	/** 
	 * 测试当前浏览器是否为Netscape
	 * @public
	 * @id System.isNetscape
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isNetscape: f
	/** 
	 * 测试当前浏览器是否为Mozilla
	 * @public
	 * @id System.isMozilla
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isMozilla: f
	/** 
	 * 测试当前浏览器是否为Firefox
	 * @public
	 * @id System.isFirefox
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isFirefox: f
	/** 
	 * 测试当前浏览器是否为KHTML核心
	 * @public
	 * @id System.isKHTML
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isKHTML: f
	/** 
	 * 测试当前浏览器是否为Safari
	 * @public
	 * @id System.isSafari
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isSafari: f
	/** 
	 * 测试当前浏览器是否为Konqueror
	 * @public
	 * @id System.isKonqueror
	 * @param {number} [arguments] 版本号
	 * @return {boolean} 测试结果
	 */
	, isKonqueror: f
	/** 
	 * 基于当前页面的唯一ID值Page Unique Identifiers
	 * @public
	 * @id System.PUID
	 * @return {string} 唯一ID
	 */
	, PUID : function()
	{
		return "_puid_" + puid++;
	}
};

System.PUID.toString = function()
{
	return "_puid_" + puid++;
}

function Version(version)
{
	var vs = version.match(/([\d\.]*)(.*)?/);
	this.flag = vs[2] ? vs[2].replace(/^\s+(.*[^\s])\s+$/, "$1")||"" : "";
	vs = vs[1].match(/(\d+(\.\d+)?)/g);
	for (var i = vs.length - 1; i >= 0; i--)
	{
		vs[i] = parseFloat(vs[i]);
	}
	this.version = version;
	this.value = vs[0];
	this.values = vs;
}

var $pv = Version.prototype;

$pv.compare = function(v)
{
	var len, i;
	if (Type.isNumber(v))
	{
		if (v == this.value)
		{
			for (i = this.values.length - 1; i >=0 ; i--)
			{
				if (this.values[i])
					return 1;
			}
			return 0;
		}
		else
		{
			return v - this.value;
		}
	}
	else
	{
		if (Type.isString(v))
		{
			if (v == this.version)
			{
				return 0;
			}
			v = new Version(v);
		}
		if (v.value == this.value)
		{
			len = Math.min(this.values.length, v.values.length);
			for (i = 0; i < len; i++)
			{
				if (this.values[i])
					return 1;
			}
			len = v.values.length - this.value.length;
			if (len > 0)
			{
				return 1;
			}
			else if (len < 0)
			{
				return  - 1;
			}
			if (v.flag == this.flag)
			{
				return 0;
			}
			else if (!v.flag)
			{
				return 1;
			}
			else if (!this.flag)
			{
				return  - 1;
			}
			else
			{
				return v.flag > this.flag ? 1 :  - 1;
			}
		}
		else
		{
			return v.value - this.value;
		}
	}

}

$pv.before = function(v)
{
	return this.compare(v) >= 0;
}

$pv.after = function(v)
{
	return this.compare(v) <= 0;
}

function bc(version)
{
	var v = new Version(version);
	function t (min, max)
	{
		if (min == null)
		{
			if (max == null)
			{
				return version;
			}
			else
			{
				return v.before(max) ? version : false;
			}
		}
		else if (max == null)
		{
			return v.after(min) ? version : false;
		}
		else
		{
			return v.after(min) && v.before(max) ? version : false;
		}
	}
	t.toString = Type.T_FUNC;
	return t;
}

if (ua.indexOf("Opera") > -1)
{
	//Opera
	System.isOpera = bc(ua.replace(/.*Opera\/*\s*([^; ]+).*/, "$1"));
}

else if (ua.indexOf(" MSIE ") > -1)
{
	//MSIE
	System.isIE = bc(ua.replace(/.*MSIE\s+([^; ]+).*/, "$1"));
}

else if (ua.indexOf("Safari") > -1)
{
	System.isKHTML = System.isKHTML.toString = Type.T_FUNC;
	System.isSafari = bc(ua.replace(/.*Safari\/([^;\/ ]+).*/, "$1"));
}

else if (ua.indexOf("Gecko/") > -1)
{
	//mozilla netscape firefox ...
	System.isGecko = bc(ua.replace(/.*Gecko[\s\/]*([^;\/\) ]+).*/, "$1"));
	if (ua.indexOf("Firefox") > -1)
	{
		System.isFirefox = bc(ua.replace(/.*Firefox[\s\/]*([^;\/\) ]+).*/, "$1")); 
	}
	else if (ua.indexOf("Netscape") > -1)
	{
		System.isNetscape = bc(ua.replace(/.*Netscape[\s\/]*([^;\/\) ]+).*/, "$1")); 
	}
	else if (ua.indexOf("Mozilla") > -1)
	{
		 System.isMozilla = bc(ua.replace(/.*rv:([^;\/\) ]+).*/, "$1")); 
	}
}

else if (ua.indexOf("KHTML") > -1)
{
	System.isKHTML = System.isKHTML.toString = Type.T_FUNC;
	//khtml
	if (ua.indexOf("Konqueror") > -1)
	{
		System.isKonqueror = bc(ua.replace(/.*Konqueror\/([^;\/ ]+).*/, "$1")); 
	}
}

$pv = ua = null;
